package models

import (
	"fly/schema"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"time"
)

// Aircraft 飞机
type Aircraft struct {
	BaseModel
	Code             string     `orm:"column(code);description(飞机编号)" json:"code"`
	DeptId           int64      `orm:"column(dept_id);description(所属部门)" json:"deptId"`
	TypeDictId       int64      `orm:"column(type_dict_id);description(飞机类型对应字典Id)" json:"typeDictId"`
	StateDictId      int64      `orm:"column(state_dict_id);description(飞机状态对应字典Id)" json:"stateDictId"`
	FactoryTime      time.Time  `orm:"column(factory_time);description(出厂时间)" json:"factoryTime"`
	FlightDuration   int        `orm:"column(flight_duration);description(飞行时间:秒)" json:"flightDuration"`
	TotalLife        float32    `orm:"column(total_life);description(总寿命:年)" json:"totalLife"`
	TotalUpDown      int        `orm:"column(total_up_down);description(总起落次数:次)" json:"totalUpDown"`
	Enabled          int        `orm:"column(enabled);description(启用状态,<0:无效；>=0:有效)" json:"enabled"`
	CombatReady      int        `orm:"column(combat_ready);description(战备状态：1-战备中)" json:"combatReady"`
	LocationId       int64      `orm:"column(location_id);description(机位id)" json:"locationId"`
	LatestFlightTime *time.Time `orm:"column(latest_flight_time);description(最新飞行时间时间)" json:"latestFlightTime"`
}

func init() {
	orm.RegisterModel(new(Aircraft))
}

// SimpleField 简略查询字段
func (*Aircraft) SimpleField() []string {
	return []string{"id", "code", "dept_id", "type_dict_id", "state_dict_id"}
}

// SearchField 定义模型的可搜索字段
func (*Aircraft) SearchField() []string {
	return []string{"code", "status", "type"}
}

// WhereField 定义模型可作为条件的字段
func (*Aircraft) WhereField() []string {
	return []string{"id", "status", "dept_id", "code"}
}

func (m *Aircraft) TableName() string {
	return "aircraft"
}

// AddAircraft insert a new Aircraft into database and returns
// last inserted Id on success.
func AddAircraft(m *Aircraft, userId int64) (id int64, err error) {
	o := orm.NewOrm()
	m.Creator = userId
	m.Updater = userId
	id, err = o.Insert(m)
	return
}

// GetAircraftById retrieves Aircraft by Id. Returns error if
// Id doesn't exist
func GetAircraftById(id int64) (v *Aircraft, err error) {
	o := orm.NewOrm()
	v = &Aircraft{BaseModel: BaseModel{Id: id}}
	if err = o.QueryTable(new(Aircraft)).Filter("id", id).RelatedSel().One(v); err == nil {
		return v, nil
	}
	return nil, err
}

// UpdateAircraftById updates Aircraft by Id and returns error if
// the record to be updated doesn't exist
func UpdateAircraftById(m *Aircraft, userId int64) (err error) {
	o := orm.NewOrm()
	v := Aircraft{BaseModel: BaseModel{Id: m.Id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		m.Updater = userId
		var num int64
		if num, err = o.Update(m); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}

// DeleteAircraft deletes Aircraft by Id and returns error if
// the record to be deleted doesn't exist
func DeleteAircraft(id int64) (err error) {
	o := orm.NewOrm()
	v := Aircraft{BaseModel: BaseModel{Id: id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Delete(&Aircraft{BaseModel: BaseModel{Id: id}}); err == nil {
			fmt.Println("Number of records deleted in database:", num)
		}
	}
	return
}

// DestroyAircraft Destroy Aircraft by Id and returns error if
func DestroyAircraft(id int64, userId int64) (err error) {
	o := orm.NewOrm()
	v := Aircraft{BaseModel: BaseModel{Id: id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		v.Status = schema.StatusDelete
		v.Updater = userId
		var num int64
		if num, err = o.Update(v, "updater", "status"); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}
